package com.android.hedgehog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.scoreloop.client.android.core.controller.RequestController;
import com.scoreloop.client.android.core.controller.RequestControllerObserver;
import com.scoreloop.client.android.core.controller.ScoreController;
import com.scoreloop.client.android.core.controller.UserController;
import com.scoreloop.client.android.core.model.Score;
import com.scoreloop.client.android.core.model.Session;
import com.scoreloop.client.android.core.model.User;

public class SubmitScore extends Activity {

	// identifiers for our dialogues
	static final int DIALOG_PROGRESS = 0;
	static final int DIALOG_SUBMITTED = 1;
	static final int DIALOG_FAILED = 2;
	double playerScore = 2000;
	Intent intent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		RequestControllerObserver observer = new RequestControllerObserver() {

			@Override
			public void requestControllerDidFail(RequestController rcontroller,
					Exception exception) {
				// something went wrong, like no internet
				Log.d("------------", "BBBBBBBBBBB222222222222");
				dismissDialog(DIALOG_PROGRESS);
				Toast.makeText(getApplicationContext(),
						"Maybe you should check connection?", Toast.LENGTH_LONG)
						.show();
				finish();

			}

			@Override
			public void requestControllerDidReceiveResponse(
					RequestController rcontroller) {
				Log.d("------------", "BBBBBBBBBB11111111111");
				dismissDialog(DIALOG_PROGRESS);
				Toast.makeText(getApplicationContext(), "Success",
						Toast.LENGTH_SHORT).show();
				intent = new Intent(SubmitScore.this, GlobalRank.class );
				startActivity(intent);

			}

		}; // REQUESTCONTROLLEROBSERVER

		Score score = new Score(playerScore, null);
		ScoreController scoreController = new ScoreController(observer);
		showDialog(DIALOG_PROGRESS);
		scoreController.submitScore(score);
		
	} // ONCREATE
	
	





	// handler to create our dialogs
	protected Dialog onCreateDialog(int id) {
		switch (id) {
			case DIALOG_PROGRESS :
				return ProgressDialog.show(SubmitScore.this, "",
						"submitting_your_score");
			case DIALOG_SUBMITTED :
				return (new AlertDialog.Builder(this))
						.setMessage("score_was_submitted")
						.setTitle("scoreloop")
						.setIcon(
								getResources().getDrawable(
										R.drawable.ic_launcher))
						.setPositiveButton("awesome", null).create();
			case DIALOG_FAILED :
				return (new AlertDialog.Builder(this))
						.setMessage("score_submit_error")
						.setPositiveButton("too_bad", null).create();
		}
		return null;
	}

}
